/**
 * Created by WT-SUN on 2017/4/24.
 */

$(function () {
    $('#accountTb').datagrid({
        rownumbers: true,//显示行号
        striped: true,
        fit: true,
        singleSelect: true,
        sortOrder: true,
        fitColumns: true,
        pagination: true,
        toolbar: '#toolbar',
        url: ROOT_PATH + '/occupationalHealth/account/AcTable.shtml',
        queryParams:{
            sort:'addTime',
            order:'desc'
        },
        columns: [[
            {
                field: 'id',
                checkbox: true
            },
            {field: 'userPhy', title: '姓名', width: 100,formatter: function (value, row, index) {
                return "<a href='javascript:void(0)' class='easyui-linkbutton text-primary' plain='true'  onclick='infoAcData(\""+row.id+"\");'>"+row.userPhy+"</a>";
            }},
            {field: 'postName', title: '岗位', width: 100},
            {field: 'diseasesName', title: '职业病名称', width: 100},
            {field: 'diseasesTypeName', title: '职业病类型', width: 100},
            {field: 'diagnosisDate', title: '诊断日期', width: 100},
            {field: 'zgTypeName', title: '转归分类', width: 100},
            {field: 'stateZdName', title: '状态', width: 100},
            {field: 'isDiagnosis', title: '是否有诊断证明', width: 100,formatter: function (value, row, index) {
                return row.isDiagnosis == '0' ? '<span class="color-success text-white" style="padding: 3px;border-radius:5px;">是</span>' : '<span class="color-important text-white" style="padding: 3px;border-radius:5px;">否</span>';
            }},
            {
                field: 'CompanyName',
                title: '所属部门',
                width: 100,
                formatter: function (value, row, index) {
                    var name = '';
                    if (row.userDepartmentList && row.userDepartmentList.length > 0) {
                        for (var index in row.userDepartmentList) {
                            if (row.userDepartmentList[index].deptName != null) {
                                name += ',' + row.userDepartmentList[index].deptName;
                            }
                        }
                        return name.substring(1);
                    } else {
                        return '';
                    }
                }
            }
            /*{field: 'userName', title: '登记人', width: 100},
            {field: 'deptName', title: '登记部门', width: 100},
            {field: 'addTime', title: '登记时间', width: 100}*/
        ]]
    });
    queryComboData();
    $('#btn_account_add').on('click',function () {
        addAccount();
    });
    $('#btn_account_delete').on('click',function () {
        deleteAccount();
    });
    $('#btn_account_edit').on('click',function () {
        editAccount();
    });
    $('#btn_advanced_search').on("click", function () {
        $('#queryForm2').toggle();
        $('#accountTb').datagrid('resize');
    });
    $('#btn_clear').on("click", function () {
        $('#queryForm1').form('reset');
        $('#queryForm2').form('reset');
    });
    var code=CONSTANT.DictionaryTypeDefConstants.OHE_HEALTH_ACSTATEZD;
    queryComboData(code,'#stateZd');
    var OHE_HEALTH_ACZGTYPE=CONSTANT.DictionaryTypeDefConstants.OHE_HEALTH_ACZGTYPE;//转归类型
    queryComboData(OHE_HEALTH_ACZGTYPE,'#zgType');
    var OHE_OCCDISEASES_TYPE=CONSTANT.DictionaryTypeDefConstants.OHE_OCCDISEASES_TYPE;//职业病类别
    queryComboData(OHE_OCCDISEASES_TYPE,'#diseasesType');
    queryDepartmentTree();
    var isDiagnosis=[{id:'',text:'-请选择-'},{id:'0',text:'是'},{id:'1',text:'否'}];
    isDandA(isDiagnosis,'#isDiagnosis');
    isDandA(isDiagnosis,'#isAppraisal');
});
function isDandA(data,id) {
    $(id).combobox({
        data: data,
        valueField: 'id',
        textField: 'text',
        editable: false,
        panelHeight: '200',
        selectOnNavigation:true,
    });
}
function queryDepartmentTree(){
    $.ajax({
        url:ROOT_PATH + '/permission/department/getDepartmentTree.shtml',//提交地址
        data:{parentId : 0},//将表单数据序列化
        type:"POST",
        dataType:"json",
        success:function(result){
            deptCom('searchDept',result);
        }
    });
}
function deptCom(id,result) {
    var deptId='#'+id;
    $(deptId).combotree(
        {
            data:result,
            require:true,
            loadFilter:function(data){
                var newData = new Array();
                newData.push({id:"",text:'请选择'});
                newData.push(data[0]);
                return newData;
            }
        }
    );
}
function queryComboData(code,id) {
    var EMPTY_OPTION = {
        id: '',
        text: '请选择'
    };
    $.ajax({
        type: 'post',
        url: ROOT_PATH + '/permission/dictionary/queryDictionaryByDictionaryTypeCode.shtml',
        data: {code: code},
        dataType: "json",
        success: function (result) {
            $(id).combobox({
                loadFilter: function (data) {
                    data.unshift(EMPTY_OPTION);
                    return data;
                },
                data: result,
                valueField: 'id',
                textField: 'text',
                editable: false,
                panelHeight: '200',
                onSelect: function (record) {
                }
            });
        }
    });
}
/**
 * 重新加载表格
 */
function reLoadData() {
    $('#accountTb').datagrid('clearSelections');
    var queryParams1 = $('#queryForm1').serializeJson();
    var queryParams2 = $('#queryForm2').serializeJson();
    var queryParams = $.extend(queryParams1,queryParams2);
    queryParams["sort"] = 'addTime';
    queryParams["order"] = 'desc';
    $('#accountTb').datagrid('options').queryParams = queryParams;
    $("#accountTb").datagrid('reload', ROOT_PATH + '/occupationalHealth/account/AcTable.shtml');
}
/**
 * 打开弹框
 * @param title
 * @param content
 */
function openLayers(title, content) {
    parent.layer.open({
        type: 2,
        resize: false,
        title: title,
        area: ['1150px', '80%'],
        content: content
    });
}
/**
 * 打开新建
 */
function addAccount() {
    var title = '新增职业病台账';
    var content = ROOT_PATH + '/occupationalHealth/account/add.shtml?resourceId=' + $.getQueryParam('resourceId');
    openLayers(title, content);
}
/**
 * 删除
 */
function deleteAccount() {
    var rows = $('#accountTb').datagrid('getSelections');

    if (rows.length == 0) {
        layer.alert("选择需要删除的行！");
        return
    }
    var id = rows[0].id;
    layer.confirm('是否确定删除当前选中数据？', {
        btn: ['确定', '取消'] //按钮
    }, function () {
        $.ajax({
            type: 'post',
            url: ROOT_PATH + '/occupationalHealth/account/deleteById.shtml',
            data: {id: id},
            dataType: "json",
            success: function (result) {
                //result = $.parseJSON(result);
                if (result.success) {
                    $('#accountTb').datagrid('clearSelections');
                    reLoadData();
                    layer.msg('删除成功', {
                        icon: 1,
                        time: 1000
                    });
                }
                else {
                    layer.msg('删除失败', {
                        icon: 2,
                        time: 1000
                    });
                }
            }
        });
    });
}
/**
 * 编辑
 */
function editAccount() {
    var rows = $('#accountTb').datagrid('getSelections');
    if (rows.length == 0) {
        layer.alert("选择需要编辑的行！");
        return
    }
    var id = rows[0].id;
    var title = '编辑职业病台账';
    var content = ROOT_PATH + '/occupationalHealth/account/info.shtml?resourceId=' + $.getQueryParam('resourceId')+'&id=' + id+'&type=edit';
    openLayers(title, content);
}
/**
 * 职业病台账详情
 * @param id
 */
function infoAcData(id) {
    var title = '职业病台账详情';
    var content = ROOT_PATH + '/occupationalHealth/account/info.shtml?resourceId=' + $.getQueryParam('resourceId')+'&id=' + id+'&type=info';
    openLayers(title, content);
}

